package timing.ukulele.third.service.msg;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import timing.ukulele.data.third.data.msg.MessageNoticeItemMappingFiledDTO;
import timing.ukulele.data.third.view.msg.MessageNoticeItemMappingFiledVO;
import timing.ukulele.third.convertor.msg.MessageNoticeItemMappingFiledConverter;
import timing.ukulele.third.exception.ThirdServiceException;
import timing.ukulele.third.mapper.msg.MessageNoticeItemMappingFiledMapper;
import timing.ukulele.third.persistent.msg.MessageKeyMappingFiled;
import timing.ukulele.third.persistent.msg.MessageNoticeItem;
import timing.ukulele.third.persistent.msg.MessageNoticeItemMappingFiled;
import timing.ukulele.third.service.common.BaseOperatorService;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 概要描述
 * @Author zh
 * @Date 2025-10-13 16:38
 */
@Service
public class MessageNoticeItemMappingFiledService extends BaseOperatorService<
        MessageNoticeItemMappingFiledMapper, MessageNoticeItemMappingFiled> {

    private final MessageKeyMappingFiledService mappingFiledService;

    public MessageNoticeItemMappingFiledService(MessageKeyMappingFiledService mappingFiledService) {
        this.mappingFiledService = mappingFiledService;
    }

    public Boolean handleByNoticeItem(MessageNoticeItem messageNoticeItem, List<String> filedList) {
        // 预先删除原数据
        this.lambdaUpdate().set(MessageNoticeItemMappingFiled::getDeleted, Boolean.TRUE)
                .eq(MessageNoticeItemMappingFiled::getNoticeItemId, messageNoticeItem.getId()).update();
        // 保存新数据
        List<MessageNoticeItemMappingFiled> insertList = new ArrayList<>();

        List<MessageKeyMappingFiled> mappingFiledList = mappingFiledService.getByFiledList(filedList);

        mappingFiledList.forEach(item -> {
            MessageNoticeItemMappingFiled insertItem = new MessageNoticeItemMappingFiled();
            insertItem.setServeId(messageNoticeItem.getServeId());
            insertItem.setNoticeItemId(messageNoticeItem.getId());
            insertItem.setNoticeItemName(messageNoticeItem.getName());
            insertItem.setMappingFiled(item.getMappingFiled());
            insertItem.setDesc(item.getDesc());
            insertList.add(insertItem);
        });
        return this.saveBatch(insertList);
    }

    public Boolean addMappingFiled(MessageNoticeItemMappingFiledDTO dto) {
        validateWriteOperation(dto);
        MessageNoticeItemMappingFiled entity = MessageNoticeItemMappingFiledConverter.INSTANCE.d2p(dto);
        return this.save(entity);
    }

    private void validateWriteOperation(MessageNoticeItemMappingFiledDTO dto) {
        if (this.lambdaQuery()
                .eq(MessageNoticeItemMappingFiled::getNoticeItemId, dto.getNoticeItemId())
                .eq(MessageNoticeItemMappingFiled::getMappingFiled, dto.getMappingFiled())
                .notIn(dto.getId() != null, MessageNoticeItemMappingFiled::getId, dto.getId()).count() > 0) {
            throw new ThirdServiceException("该通知事项下字段已存在，请勿重复添加");
        }
    }

    public Boolean updateMappingFiled(MessageNoticeItemMappingFiledDTO dto) {
        validateWriteOperation(dto);
        MessageNoticeItemMappingFiled entity = MessageNoticeItemMappingFiledConverter.INSTANCE.d2p(dto);
        return this.updateById(entity);
    }

    public Boolean deleteMappingFiled(Long id) {
        return this.lambdaUpdate().set(MessageNoticeItemMappingFiled::getDeleted, Boolean.TRUE)
                .eq(MessageNoticeItemMappingFiled::getId, id).update();
    }

    /**
     * 分页列表
     * @param dto
     * @return
     */
    public IPage<MessageNoticeItemMappingFiledVO> pageList(MessageNoticeItemMappingFiledDTO dto) {
        Page pageParam = new Page(dto.getCurrent(), dto.getPageSize());
        Page page = this.lambdaQuery()
                .eq(dto.getNoticeItemId() != null, MessageNoticeItemMappingFiled::getNoticeItemId, dto.getNoticeItemId())
                .like(StrUtil.isNotBlank(dto.getMappingFiled()), MessageNoticeItemMappingFiled::getMappingFiled, dto.getMappingFiled())
                .eq(MessageNoticeItemMappingFiled::getDeleted, Boolean.FALSE)
                .page(pageParam);
        List list = MessageNoticeItemMappingFiledConverter.INSTANCE.p2vList(page.getRecords());
        page.setRecords(list);
        return page;
    }

    public List<MessageNoticeItemMappingFiledVO> getByNoticeItemId(Long noticeItemId) {
        List<MessageNoticeItemMappingFiled> list = this.lambdaQuery()
                .eq(MessageNoticeItemMappingFiled::getNoticeItemId, noticeItemId)
                .eq(MessageNoticeItemMappingFiled::getDeleted, Boolean.FALSE)
                .list();
        return MessageNoticeItemMappingFiledConverter.INSTANCE.p2vList(list);
    }

    public List<String> getFiledByNoticeItemId(Long noticeItemId) {
        return this.lambdaQuery()
                .eq(MessageNoticeItemMappingFiled::getNoticeItemId, noticeItemId)
                .eq(MessageNoticeItemMappingFiled::getDeleted, Boolean.FALSE)
                .list().stream().map(item -> item.getMappingFiled()).collect(Collectors.toList());
    }

}
